package com.zdsoft.blp.zongbuju.shouye.linliquan.activity;

import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.zdsoft.blp.R;
import com.zdsoft.blp.gongyong.API.API;
import com.zdsoft.blp.gongyong.camera.Camera;
import com.zdsoft.blp.gongyong.fanhui.FanHuiView;
import com.zdsoft.blp.gongyong.net.BaseActivity;
import com.zdsoft.blp.gongyong.net.Net;
import com.zdsoft.blp.gongyong.net.NetCallback;
import com.zdsoft.blp.gongyong.net.UpLoadFiles;
import com.zdsoft.blp.gongyong.upLoad.Base64ToString;
import com.zdsoft.blp.zongbuju.shouye.linliquan.adapter.FBgvAdapter;
import com.zdsoft.blp.zongbuju.shouye.wuye.adapter.GridAdapter;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2016/9/1.
 */
public class FBLTActivity extends BaseActivity implements NetCallback{
    private FanHuiView fh;
    private TextView tv_fabu;
    private EditText et_title,et_content;
    private GridView gv;
    private ImageView iv_xiangji;
    private List<Bitmap> list;
    private FBgvAdapter adapter;
    private String title,content;

    @Override
    protected void init() {
        setContentView(R.layout.linli_luntanfabu);
        fh=find(R.id.fh);
        fh.getTv_title().setText("社区论坛");
        tv_fabu=find(R.id.tv_fabu);
        et_title=find(R.id.et_title);
        et_content=find(R.id.et_content);
        gv=find(R.id.gv);
        iv_xiangji=find(R.id.iv_xiangji);

        list = new ArrayList<>();
        adapter = new FBgvAdapter(list, getApplicationContext());
        gv.setAdapter(adapter);
        gv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (list.size()==position){
                    Camera.setOnClickListener(iv_xiangji, FBLTActivity.this);
                }
            }
        });
        tv_fabu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                title= et_title.getText().toString();
                 content= et_content.getText().toString();
                if(!title.equals("")){
                    UpLoadFiles.upLoadFiles(list, handler, null);
                }else {
                    Toast.makeText(FBLTActivity.this,"内容不能为空",Toast.LENGTH_SHORT).show();

                }


            }
        });
    }

    private void getNet(String picId) {
        SharedPreferences sp = getSharedPreferences("info", Context.MODE_PRIVATE);

        Net net = new Net();
        net.url= API.BBS_URL;
        net.nameSpace= API.BBS_NAMESPACE;
        net.methodName = API.BBSSAVE;
        Map<String,Object> map = new HashMap<>();
        map.put("title", title);
        map.put("content", content);
        map.put("tempFileIds", picId);
        map.put("comIds", "");
        map.put("userId", sp.getString("userId",""));
        net.parms =map;
        getServer(net,FBLTActivity.this);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        Camera.dialog.dismiss();
        switch (requestCode) {
            case Camera.PAI:
                if (resultCode != RESULT_CANCELED) {
                    try {
                        FileInputStream in = new FileInputStream(Environment.getExternalStorageDirectory() + "/image.jpg");
                        Bitmap bitmap = BitmapFactory.decodeStream(in);
                        list.add(bitmap);
                        adapter.notifyDataSetChanged();
                    } catch (FileNotFoundException e) {
                        e.printStackTrace();
                    }
                }
                break;
            case Camera.XUAN:
                if (resultCode != RESULT_CANCELED) {
                    ContentResolver resolver = getContentResolver();
                    InputStream in = null;
                    try {
                        in = resolver.openInputStream(data.getData());
                    } catch (FileNotFoundException e) {
                        e.printStackTrace();
                    }
                    Bitmap bitmap = BitmapFactory.decodeStream(in);
                    list.add(bitmap);
                    adapter.notifyDataSetChanged();
                }
                break;
        }
    }

    @Override
    public void onResponse(boolean flag, int code, String res) {
        if (flag){
            try {
                JSONObject object=new JSONObject(res);
                String msg=object.getString("msg");
                Toast.makeText(FBLTActivity.this,msg,Toast.LENGTH_SHORT).show();
            } catch (JSONException e) {
                e.printStackTrace();
            }

        }

    }
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == 2) {
                if (!"".equals(msg.obj.toString())) {
                    getNet(msg.obj.toString());
                }
            }
        }
    };
}
